﻿var ns = namespace("kaskelot.sp.ui");

ns.adminval = function () {

	function getList() {
		$.ajax({
			url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientAdminService.svc/GetAllVal",
			type: "GET",
			data: {},
			processData: true,
			cache: false,
			success:
                function (data) {
                	if (!data.GetAllValResult.HasError) {
                		$("#val-results tbody").html(
							$("#val-template").render(data.GetAllValResult.Data)
						);
                	}
                },
			error:
                function (xhr) {
                	kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
                }
		});
	}

	function showCreateForm() {
		var selector = "#createval-form";
		kaskelot.sp.ui.formutils.getAndShowForm({
			params: {
				formName: "CreateValForm",
				idString: "",
				parentIdString: ""
			},
			selector: selector,
			openFunction: function () {
				$(".chzn-select-deselect").chosen({ disable_search: true });
				$("#ValDatum").datepicker();
			},
			okFunction: function () {
				$(selector).parent().block();

				var prog = kaskelot.sp.ui.formutils.getObject(selector);
				var data = "{\"form\":" + JSON.stringify(prog) + "}";

				var method = "CreateVal";

				$.ajax({
					url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientAdminService.svc/" + method,
					data: data,
					type: "POST",
					processData: false,
					contentType: "application/json",
					dataType: "json",
					success: function (resp) {
						if (resp[method + "Result"].HasError) {
							kaskelot.sp.ui.formutils.showError(selector, resp[method + "Result"].ErrorMessage);
						}
						else {
							$(selector).dialog("close");
							getList();
						}
					},
					error: function (xhr) {
						kaskelot.sp.ui.formutils.showError(selector, xhr.responseText);
					}
				});
			}
		});
	}

	function showEditForm(id) {
		var selector = "#val-form";
		kaskelot.sp.ui.formutils.getAndShowForm({
			params: {
				formName: "ValForm",
				idString: id,
				parentIdString: ""
			},
			selector: selector,
			openFunction: function () {
				$(selector).dialog({ title: "Redigera val" });
				$("#ValDatum").datepicker();
			},
			okFunction: function () {
				$(selector).parent().block();

				var prog = kaskelot.sp.ui.formutils.getObject(selector);
				var data = "{\"form\":" + JSON.stringify(prog) + "}";

				var method = "UpdateVal";

				$.ajax({
					url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientAdminService.svc/" + method,
					data: data,
					type: "POST",
					processData: false,
					contentType: "application/json",
					dataType: "json",
					success: function (resp) {
						if (resp[method + "Result"].HasError) {
							kaskelot.sp.ui.formutils.showError(selector, resp[method + "Result"].ErrorMessage);
						}
						else {
							$(selector).dialog("close");
							getList();
						}
					},
					error: function (xhr) {
						kaskelot.sp.ui.formutils.showError(selector, xhr.responseText);
					}
				});
			}
		});
	}

	function setActive(id) {
		var valId = id;
		var method = "SetActive";

		$.ajax({
			url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientAdminService.svc/" + method,
			data: { id: valId },
			type: "GET",
			processData: true,
			contentType: "application/json",
			dataType: "json",
			cache: false,
			success: function (resp) {
				if (resp[method + "Result"].HasError) {
					alert(resp[method + "Result"].ErrorMessage);
				}
				else {
					window.location.reload();
				}
			},
			error: function (xhr) {
				alert(xhr.responseText);
			}
		});
	}

	return {
		getList: getList,
		showEditForm: showEditForm,
		showCreateForm: showCreateForm,
		setActive: setActive
	};
}();